#!/usr/bin/perl
# 
#  Exercise 6.6 
# 
# Use Exercise 6.3 in a subroutine that keeps prompting until a valid file is entered 
# by the user or until five attempts have failed.
#
# Answer to Exercise 6.6

use strict;
use warnings;



my $message = "Please type something here: ";

my $answer = persistentQandA( $message );

if($answer) {
	print $answer;
}else{
	print "You didn\'t reply! Goodbye.\n";
	exit;
}

exit;

sub QandA {
	my($message) = @_;

	print $message;

	my $ans = <STDIN>;

	return $ans;
}

sub persistentQandA {

	my($message) = @_;

	my $ans = QandA($message);

	my $count = 1;
	while($count < 5 and $ans =~ /^\s*\n/) {
		$count++;
		$ans = QandA($message);
	}

	if($ans =~ /^\s*\n/) {
		return 0;
	}else{
		return $ans;
	}
}
